package cn.edu.zafu.bigdata.yt.wlbc;

import java.net.*;

public class TimeClient_yt {
    public static void main(String[] args) {
        String serverAddress = "127.0.0.1"; 
        int port = 12345; 

        try (DatagramSocket clientSocket = new DatagramSocket()) {
            byte[] sendBuffer = "GET_TIME".getBytes();
            byte[] receiveBuffer = new byte[1024];

            InetAddress serverInetAddress = InetAddress.getByName(serverAddress);
            DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, serverInetAddress, port);
            clientSocket.send(sendPacket);
            System.out.println("Request sent to server.");

            DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
            clientSocket.receive(receivePacket);
            String serverTime = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("Server time: " + serverTime);
        } catch (Exception e) {
            System.out.println("Client error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
